
<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

require("frmCookie.php");

$iOpcion = $_GET[iOpcion];

switch ($iOpcion)
{
	case 1:
	{
		$mySQL = "SELECT S.idStock, S.cantidad, S.imei, P.descripcionFabricante, P.codigoTango FROM stock S, partes P, tiposusosxstock TUXS WHERE S.idStock = TUXS.idStock AND TUXS.idTipoUso=3 AND S.idParte = P.idParte AND S.idAlmacen=" . $_POST[txtOrigenDef];

		$result = readSQL($mySQL);
	
        foreach($result as $value)
        {
        	$field = "txt" . $value[0] . "hasta";	

        	if($_POST[$field] != "")
        	{
        		$mySQLStock = "SELECT * FROM stock WHERE idStock=" . $value[0];
        		$resultStock = readSQL($mySQLStock);
        		
        		foreach($resultStock as $valueStock)
        		{
        			$mySQLStockVenta = "INSERT INTO ventas(nroFactura, idParte, imei, cantidad) VALUES(";
        			$mySQLStockVenta = $mySQLStockVenta . "'" . $_POST[txtFacturaDef] . "', ";
        			$mySQLStockVenta = $mySQLStockVenta . $valueStock['idParte'] . ", ";
        			$mySQLStockVenta = $mySQLStockVenta . "'" . $valueStock['imei'] . "', ";
        			$mySQLStockVenta = $mySQLStockVenta . $_POST[$field] . ")";
        			echo $mySQLStockVenta . "<br>";
        			executeSQL($mySQLStockVenta);
        			
        			$mySQLSelectStock = "SELECT cantidad FROM stock WHERE idStock=" . $value[0];
            		$resultSelectStock = readSQL($mySQLSelectStock);
					foreach($resultSelectStock as $valueSelectStock)
					{
						$total = $valueSelectStock[0] - $_POST[$field];
						$mySQLUpdateStock = "UPDATE stock SET cantidad = " . $total . " WHERE idStock = " . $value[0];
						//echo $mySQLUpdateStock . "<br>";
						executeSQL($mySQLUpdateStock);
					}
        		}
        	}
        }

        ?>
		  <script language="JavaScript" type="text/javascript">
		  <!--
		  window.location.href = "venta.php?iOpcion=1&iMensaje=1"
		  //-->
		  </script>
		<?php
        //header("Location: venta.php?iOpcion=1&iMensaje=1");
		break;
	}
}
?>
